package xc.src.demo;

//多个线程同一个对象
//买火车票的例子

//多个线程操作同一资源 ---不安全
public class testThread4 implements Runnable{

//票数
    int ticketnumbers =10;

    @Override
    public void run(){
        while (true){

            if (ticketnumbers<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketnumbers--+"张票");
        }
    }

    public static void main(String[] args) {
        testThread4 ticket = new testThread4();
        new Thread(ticket,"小明").start();
        new Thread(ticket,"小红").start();
        new Thread(ticket,"小李").start();
    }
}
